package com.bwie.ai.func;

import com.bwie.ai.remote.RemotDocker;
import com.bwie.common.core.constant.Constants;
import com.bwie.common.core.domain.Result;
import com.bwie.doctor.domain.vo.MedDoctorVo;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.core.NestedExceptionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Configuration
@Log4j2
public class BookingTools {

    @Autowired
    private RemotDocker remotDocker;


    @JsonInclude(JsonInclude.Include.NON_NULL)
    public record DoctorVO(Long doctorId, String realName, String hospital, String department, String title, String speciality, String Introduction, String careerExperience,
                           BigDecimal doctorPrice, String symptomName){

    }

    public record getDoctorBySymptomRequest(String symptomName) {

    }

    public record register(String dockerName, String patientName) {

    }

    public record getRegisterByPatientName(String patientName) {

    }

    @Bean
    @Description("根据用户症状获取医生列表")
    public Function<getDoctorBySymptomRequest, List<DoctorVO>> getDoctorBySymptom(){
        return getDoctorBySymptomRequest -> {
            List<MedDoctorVo> medDoctorVos = remotDocker.queryDoctorList(getDoctorBySymptomRequest.symptomName());
            List<DoctorVO> doctorVOS = medDoctorVos.stream()
                    .map(medDoctorVo -> new DoctorVO(
                            medDoctorVo.getDoctorId(),
                            medDoctorVo.getRealName(),
                            medDoctorVo.getHospital(),
                            medDoctorVo.getDepartment(),
                            medDoctorVo.getTitle(),
                            medDoctorVo.getSpeciality(),
                            medDoctorVo.getIntroduction(),
                            medDoctorVo.getCareerExperience(),
                            medDoctorVo.getDoctorPrice(),
                            medDoctorVo.getSymptomName()
                    ))
                    .collect(Collectors.toList());
            return doctorVOS;
        };
    }

    @Bean
    @Description("根据医生姓名挂号")
    public Function<register, String> register(){
        return register -> {
            try {
                Result result = remotDocker.register(register.dockerName, register.patientName);
                if (result.getCode() != Constants.SUCCESS){
                    return result.getMsg();
                }
                return "挂号成功";
            } catch (Exception e) {
                log.info("挂号失败，失败信息：{}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return "挂号失败";
            }
        };
    }

    @Bean
    @Description("根据医生姓名取消挂号")
    public Function<register, String> cancelRegister(){
        return register -> {
            try {
                Result result = remotDocker.cancelRegister(register.dockerName, register.patientName);
                if (result.getCode() != Constants.SUCCESS){
                    return result.getMsg();
                }
                return "退号成功";
            } catch (Exception e) {
                log.info("退号失败，失败信息：{}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return "退号失败";
            }
        };
    }

    @Bean
    @Description("根据医生患者姓名查询挂号状态")
    public Function<register, Integer> getRegisterState(){
        return register -> {
            try {
                return remotDocker.getRegisterState(register.dockerName, register.patientName);
            } catch (Exception e) {
                log.info("获取挂号状态失败，失败信息：{}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return 10;
            }
        };
    }

    @Bean
    @Description("根据挂号人姓名查询挂号记录")
    public Function<getRegisterByPatientName, Integer> getRegisterByPatientName(){
        return register -> {
            try {
                return remotDocker.getRegisterByPatientName(register.patientName);
            } catch (Exception e) {
                log.info("根据患者姓名查询挂号记录，失败信息：{}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return 10;
            }
        };
    }
}
